﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using PotatoSoft.Middleware;
using GalaSoft.MvvmLight.Messaging;
using PotatoSoft.ViewModel.Messages;
using System.ComponentModel;

namespace PotatoSoft.ViewModel
{
    public class SplashScreenViewModel : ViewModelBase
    {
        public SplashScreenViewModel()
        {
            //Messenger.Default.Register<AppStartMessage>(this, HandleAppStart);
        }
        
        private VisualOctStudioCore voctsCore = VisualOctStudioCore.GetInstance();
        
        #region SplashScreenInfo
        public const string SplashScreenInfoPropertyName = "SplashScreenInfo";
        private string splashScreenInfo = "";
        public string SplashScreenInfo
        {
            get { return splashScreenInfo; }
            set
            {
                if (splashScreenInfo == value)
                    return;
                splashScreenInfo = value;
                RaisePropertyChanged(SplashScreenInfoPropertyName);
            }
        }
        #endregion
        
        #region ProgressBarMax
        public const string ProgressBarMaxPropertyName = "ProgressBarMax";
        private int progressBarMax = 0;
        public int ProgressBarMax
        {
            get { return progressBarMax; }
            set
            {
                if (progressBarMax == value)
                    return;
                progressBarMax = value;
                RaisePropertyChanged(ProgressBarMaxPropertyName);
            }
        }
        #endregion

        #region ProgressBarValue
        public const string ProgressBarValuePropertyName = "ProgressBarValue";
        private int progressBarValue = 0;
        public int ProgressBarValue
        {
            get { return progressBarValue; }
            set
            {
                if (progressBarValue == value)
                    return;
                progressBarValue = value;
                RaisePropertyChanged(ProgressBarValuePropertyName);
            }
        }
        #endregion

        #region StartCommand
        private RelayCommand startCommand;
        public RelayCommand Start
        {
            get
            {
                if (startCommand == null)
                {
                    startCommand=new RelayCommand(()=>
                    {
                        BackgroundWorker startWorker = new BackgroundWorker();
                        startWorker.DoWork+=new DoWorkEventHandler(startWorker_DoWork);
                        startWorker.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(startWorker_RunWorkerCompleted);
                        startWorker.RunWorkerAsync();
                    });
                }
                return startCommand;
            }
        }

        private void startWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            voctsCore.SetOnLoadedLibraries(new VisualOctStudioCore.OnLoadedLibraryDelegate(OnLoadLibrary));
            voctsCore.LoadLibraries();
        }

        private void startWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Messenger.Default.Send<AppStartMessage>(new AppStartMessage());
        }

        private void OnLoadLibrary(string libraryName, int libraryToLoad)
        {
            if (progressBarMax == 0)
                ProgressBarMax = libraryToLoad;
            ProgressBarValue = progressBarMax - libraryToLoad;
            SplashScreenInfo = libraryName+" loaded";
            System.Threading.Thread.Sleep(100);
            if (libraryToLoad == 0)
            {
                SplashScreenInfo = "Load Visual OCT Studio main window...";
                System.Threading.Thread.Sleep(100);
            }
        }
        #endregion
    }
}
